//
//  ActPayrollCalculatorFeeViewController.m
//  ActSistant
//
//  Created by Loan Dinh on 2/27/13.
 //

#import "ActPayrollCalculatorFeeViewController.h"
#import "NavigationView.h"
#import <QuartzCore/QuartzCore.h>
#import "ActGlobal.h"
@class AtcPayrollCalculationFeeCellWrapper;
@implementation AtcPayrollCalculationFeeCellWrapper
@synthesize celAccessory, cellSubtitle, cellTitle;
- (id)initWithTitle:(NSString *)title andSubtitle:(NSString *)subtitle withAccessory:(NSString *)accessText{
    self = [super init];
    if (self){
        cellTitle  = title;
        cellSubtitle = subtitle;
        celAccessory = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
        [celAccessory setFont:UIFontSystem(14)];
        [celAccessory setTextColor:[UIColor darkGrayColor]];
        [celAccessory setText:accessText];
    }
    return self;
}

-(void)dealloc{
    [celAccessory release];
    [super dealloc];
}
@end
@interface ActPayrollCalculatorFeeViewController ()
{
    ActPayrollCaculatorType _type;
}
@end

@implementation ActPayrollCalculatorFeeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(id)initWithType:(ActPayrollCaculatorType)type{
    self = [super initWithNibName:@"ActPayrollCalculatorFeeViewController"  bundle:nil];
    if (self) {
        _type = type;
        if(_type == ActPayrollCaculatorTypeCustomsAllowance){
//            [_titleName setText:@"Custom"];
            
        }else{
//            [_titleName setText:@"Add"];
        }
    }
    return self;

}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setSubviews];
    [self loadData];
}
-(void)setSubviews{
    CGRect mainView = self.view.bounds;
    mainView.origin.y = kLocalNavigationHeigh;
    mainView.size.height -= kLocalNavigationHeigh;
    CGFloat originY = kLocalNavigationHeigh;
    
    [self.navigationView.btnBack addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationView.btnRight1 addTarget:self action:@selector(btnSettingClicked) forControlEvents:UIControlEventTouchUpInside];
    
//first view
    _firstView = [[UIView alloc] initWithFrame:CGRectMake(mainView.origin.x, mainView.origin.y,mainView.size.width, 2*(kLocalTextFieldHeigh+kLocalPaddingView)+kLocalAddButtonHeigh+kLocalTitleHeight+kLocalSegmentHeigh)];
    [_firstView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:_firstView];
    //Title view
    CGRect titleViewFrame = CGRectMake(0,0, mainView.size.width, kLocalTitleHeight);
    UIView* titleView = [[UIView alloc] initWithFrame:titleViewFrame];
    UIImageView *imageTitle = [[UIImageView alloc] initWithFrame:titleViewFrame];
    [imageTitle setImage:[UIImage imageNamed:@"paymentbackground"]];
    [titleView addSubview:imageTitle];
    [imageTitle release];
    UILabel *titleLabel  = [[UILabel alloc] initWithFrame:titleViewFrame];
    [titleLabel setBackgroundColor:[UIColor clearColor]];
    if(_type ==ActPayrollCaculatorTypeFree){
        [titleLabel setText:@"Add"];
    }else{
        [titleLabel setText:@"Custom Allowance"];
    }
    [titleLabel setTextAlignment:UITextAlignmentCenter];
    [titleLabel setTextColor:[UIColor whiteColor]];
    [titleLabel setFont:UIFontSystem(24)];
    [titleView addSubview:titleLabel];
    [titleLabel release];
    [_firstView addSubview:titleView];
    [titleView release];
    [_firstView setBackgroundColor:[UIColor clearColor]];
    CGRect textFieldFrame = CGRectMake(0,titleViewFrame.size.height+10, mainView.size.width, kLocalTextFieldHeigh);
    UIView *firstTextFieldView  = [[UIView alloc] initWithFrame:textFieldFrame];
    
    UIImageView *textFieldFirstImage = [[UIImageView alloc] initWithFrame:CGRectMake(kLocalTextFieldOriginX,0, kLocalTextFieldWidth, kLocalTextFieldHeigh)];
    [textFieldFirstImage setImage:[UIImage imageNamed:@"fee_text_label"]];
    [firstTextFieldView addSubview:textFieldFirstImage];
    [textFieldFirstImage release];
    
    UITextField *firstTextField = [[UITextField alloc] initWithFrame:CGRectMake(kLocalTextFieldOriginX+10,10, kLocalTextFieldWidth-20, kLocalTextFieldHeigh)];
    firstTextField.delegate = self;
    if(_type == ActPayrollCaculatorTypeCustomsAllowance){
        [firstTextField setPlaceholder:@"Allowance Name"];
    }else{
        [firstTextField setPlaceholder:@"Acency/Manager Name"];
    }
    [firstTextFieldView addSubview:firstTextField];
    [firstTextField release];
    [_firstView addSubview:firstTextFieldView];
    [firstTextFieldView release];
    
    
    textFieldFrame.origin.y += kLocalTextFieldHeigh+10;
    UIView *secondTextFieldView = [[UIView alloc] initWithFrame:textFieldFrame];
    UIImageView *textFieldSecondImage = [[UIImageView alloc] initWithFrame:CGRectMake(kLocalTextFieldOriginX,0, kLocalTextFieldWidth, kLocalTextFieldHeigh)];
    [textFieldSecondImage setImage:[UIImage imageNamed:@"fee_text_label"]];
    [secondTextFieldView addSubview:textFieldSecondImage];
    [textFieldSecondImage release];
    
    UITextField *secondTextField = [[UITextField alloc] initWithFrame:CGRectMake(kLocalTextFieldOriginX+10,10, kLocalTextFieldWidth-20, kLocalTextFieldHeigh)];
    secondTextField.delegate = self;
    if(_type == ActPayrollCaculatorTypeCustomsAllowance){
        [secondTextField setPlaceholder:@"Amount"];
    }else{
        
        [secondTextField setPlaceholder:@"Fee Amount"];
    }
    [secondTextFieldView addSubview:secondTextField];
    [secondTextField release];
    [_firstView addSubview:secondTextFieldView];
    [secondTextFieldView release];
    CGRect segmentFrame = CGRectZero;
    if(_type == ActPayrollCaculatorTypeFree){
        segmentFrame = CGRectMake(0, textFieldFrame.origin.y + textFieldFrame.size.height+10, mainView.size.width, kLocalSegmentHeigh);
    }
    if(_type == ActPayrollCaculatorTypeFree){
        _segmentControl = [[UISegmentedControl alloc]  initWithItems:[NSArray arrayWithObjects:@"PERCENT",@"FLAT FEE", nil]];
        [_segmentControl addTarget:self action:@selector(doSegmentControlChanged:) forControlEvents:UIControlEventValueChanged];
        UIView *segmentView = [[UIView alloc] initWithFrame:segmentFrame];

        [segmentView addSubview:_segmentControl];
        _segmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
        [_segmentControl setFrame:CGRectMake(( mainView.size.width-kLocalSegmentWidth)/2, 0, kLocalSegmentWidth, kLocalSegmentHeigh)];
        [_segmentControl setBackgroundColor:[UIColor clearColor]];
        [_segmentControl setBackgroundImage:[Util imageWithColor:[UIColor clearColor]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [_segmentControl setBackgroundImage:[Util imageWithColor:kSelectionColor] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
        [_segmentControl setBackgroundImage:[Util imageWithColor:kSelectionColor] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
        [_segmentControl setDividerImage:[Util imageWithColor:[UIColor clearColor]] forLeftSegmentState:UIControlStateApplication rightSegmentState:UIControlStateApplication barMetrics:UIBarMetricsDefault];
        [_segmentControl setClipsToBounds:true];
        NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                          UIFontSystem(16),UITextAttributeFont,
                                          [UIColor lightGrayColor], UITextAttributeTextColor,
                                          [UIColor clearColor], UITextAttributeTextShadowColor,
//                                          [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
                                          nil];//[NSDictionary dictionaryWithObject:  [UIColor redColor]forKey:UITextAttributeTextColor];
        [_segmentControl setTitleTextAttributes:normalAttributes forState:UIControlStateNormal];
        
        NSDictionary *selectedAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                            UIFontSystem(16),UITextAttributeFont,
                                            [UIColor whiteColor], UITextAttributeTextColor,
                                            [UIColor clearColor], UITextAttributeTextShadowColor,
//                                            [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
                                            nil] ;//[NSDictionary dictionaryWithObject:  [UIColor redColor]forKey:UITextAttributeTextColor];
        [_segmentControl setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
        [_segmentControl.layer setBorderColor:[UIColor lightGrayColor].CGColor];
        [_segmentControl.layer setCornerRadius:8];
        [_segmentControl.layer setBorderWidth:0.2];
        [_segmentControl.layer setShadowColor:[UIColor whiteColor].CGColor];
        [_segmentControl.layer setShadowOffset:CGSizeMake(0, 0)];
        [_segmentControl.layer setShadowRadius:0];
        [_segmentControl.layer setShadowOpacity:0];
        [_firstView addSubview:segmentView];
        [segmentView release];
    }
    
    CGRect addButtonFrame = CGRectMake(kLocalAddButtonOriginX,textFieldFrame.origin.y+textFieldFrame.size.height+segmentFrame.size.height+20, kLocalAddButtonWidth, kLocalAddButtonHeigh);
    addButton = [[UIButton alloc] initWithFrame:addButtonFrame];
    [addButton setBackgroundImage:[UIImage imageNamed:@"fee_add_button"] forState:UIControlStateNormal];
    [addButton setBackgroundColor:[UIColor clearColor]];
    [addButton.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [addButton.layer setCornerRadius:8];
    [addButton.layer setBorderWidth:1];
    [addButton addTarget:self action:@selector(doAdd:) forControlEvents:UIControlEventTouchUpInside];
    [addButton setTitle:@"ADD" forState:UIControlStateNormal];
    [addButton setTitle:@"ADD" forState:UIControlStateHighlighted];
    [addButton.titleLabel setFont:UIFontSystem(17)];
    [addButton.titleLabel setTextColor:[UIColor darkGrayColor]];
    [_firstView addSubview:addButton];
    
    CGRect firstViewFrame = _firstView.frame;
    firstViewFrame.size.height = addButtonFrame.size.height + addButtonFrame.origin.y+10;
    [_firstView setFrame:firstViewFrame];
    //secondview
    originY +=  _firstView.frame.size.height;
    CGRect secondViewFrame = CGRectMake(0, _firstView.frame.origin.y+ _firstView.frame.size.height, mainView.size.width,mainView.size.height - _firstView.frame.size.height);
    _secondView  = [[UIView alloc] initWithFrame:secondViewFrame];
    [self.view addSubview:_secondView];
    UIImageView *titleTable = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, secondViewFrame.size.width, kLocalTableHeaderHeigh)];
    [titleTable setImage:[UIImage imageNamed:@"paymentbackground"]];
    [titleLabel setBackgroundColor:[UIColor clearColor]];
    [_secondView addSubview:titleTable];
    [titleTable release];
    secondViewFrame.origin.y = kLocalTableHeaderHeigh;
    secondViewFrame.size.height = (_secondView.frame.size.height - kLocalTableHeaderHeigh) - 88;
    _tableView = [[UITableView alloc] initWithFrame:secondViewFrame style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
    [_secondView addSubview:_tableView];
}
-(void)dealloc{
    [addButton release];
    [_segmentControl release];
    [_firstView release];
    [_secondView release];
    [_tableView release];
    [_navigationView release];
    [super dealloc];
}
-(void)loadData{
    if(!_tableData){
        _tableData = [[NSMutableArray array] retain];
    }
    [_tableData removeAllObjects];
    if(_type == ActPayrollCaculatorTypeCustomsAllowance){
        AtcPayrollCalculationFeeCellWrapper *cell1 = [[AtcPayrollCalculationFeeCellWrapper alloc] initWithTitle:@"James Cameron Allowance" andSubtitle:@"Includes Deep Sea Submarines" withAccessory:@"$1.5b"];
        [_tableData addObject:cell1];
        [cell1 release];
        AtcPayrollCalculationFeeCellWrapper *cell2 = [[AtcPayrollCalculationFeeCellWrapper alloc] initWithTitle:@"James Taylor Allowance" andSubtitle:@"Glitch Vocals" withAccessory:@"$5"];
        [_tableData addObject:cell2];
        [cell2 release];
    }else{
        AtcPayrollCalculationFeeCellWrapper *cell1 = [[AtcPayrollCalculationFeeCellWrapper alloc] initWithTitle:@"Herve Bizira" andSubtitle:@"BizCorp" withAccessory:@"30%"];
        [_tableData addObject:cell1];
        [cell1 release];
        AtcPayrollCalculationFeeCellWrapper *cell2 = [[AtcPayrollCalculationFeeCellWrapper alloc] initWithTitle:@"Marcus Addison" andSubtitle:@"Addison Technology" withAccessory:@"25%"];
        [_tableData addObject:cell2];
        [cell2 release];
        AtcPayrollCalculationFeeCellWrapper *cell3 = [[AtcPayrollCalculationFeeCellWrapper alloc] initWithTitle:@"Jonathan Moore" andSubtitle:@"Moore, Inc" withAccessory:@"10%"];
        [_tableData addObject:cell3];
        [cell3 release];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Textfield delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
#pragma mark - Buttons
-(IBAction)doSegmentControlChanged:(id)sender{
    UISegmentedControl *segment = (UISegmentedControl*)sender;
    int selectedIndex  = segment.selectedSegmentIndex;
    if(selectedIndex == 0){
        [[segment.subviews objectAtIndex:1] setTintColor:kSelectionColor];
        [[segment.subviews objectAtIndex:0] setTintColor:[UIColor whiteColor]];
    }else{
        [[segment.subviews objectAtIndex:0] setTintColor:kSelectionColor];
        [[segment.subviews objectAtIndex:1] setTintColor:[UIColor whiteColor]];
    }
    
}
-(IBAction)doAdd:(id)sender{
    addButton.selected = YES;// !((UIButton*)sender).selected;
    if(!addButton.selected){
        [addButton setBackgroundColor:[UIColor clearColor]];
        [addButton.titleLabel setTextColor:[UIColor darkGrayColor]];
    }else{
        [addButton setBackgroundColor:kSelectionColor];
        [addButton.titleLabel setTextColor:[UIColor whiteColor]];
    }
}
-(IBAction)doBack:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void) btnSettingClicked{
    ActSettingViewController *settingVC = [[[ActSettingViewController alloc] init] autorelease];
    [self.navigationController pushViewController:settingVC animated:true];
}
#pragma mark - tablview
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _tableData.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        [cell.textLabel setFont:UIFontSystem(14)];
        [cell.detailTextLabel setFont:UIFontSystem(10)];
        [cell.textLabel  setTextColor:[UIColor darkGrayColor]];
        [cell.detailTextLabel  setTextColor:[UIColor darkGrayColor]];
        UIView *cellSelection = [[UIView alloc] initWithFrame:cell.frame];
        [cellSelection setBackgroundColor:kSelectionColor];
        cell.selectedBackgroundView = cellSelection;
        [cellSelection release];
    }
    if(indexPath.row < [_tableData count]){
        AtcPayrollCalculationFeeCellWrapper *celldata = [_tableData objectAtIndex:indexPath.row];
        [cell.textLabel setText:celldata.cellTitle];
        [cell.detailTextLabel setText:celldata.cellSubtitle];
        cell.accessoryView = celldata.celAccessory;
    }
    [cell setClipsToBounds:true];
    return cell;
}
- (void)viewDidUnload {
    [self setNavigationView:nil];
    [super viewDidUnload];
}
@end
